package com.scalar.chiptrack.operations.dataentry.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.dataentry.forms.BOMDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.BOMLotBinData;
import com.scalar.chiptrack.operations.dataentry.dao.BOMDataEntryDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * User: Murali Krishna Yengala
 * Date: Jul 5, 2005
 * Time: 3:22:54 PM
 */
public class BOMBinLotDataEntryAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/bomDataEntry.do";

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) throws Exception
    {

        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if ( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME );
            return ( mapping.findForward( "login" ) );
        }


        HashMap errors                      = new LinkedHashMap();
        HashMap info                        = new LinkedHashMap();
        String actionForward                = "bomLotBinDataEntry";
        String action                       = request.getParameter( "action" );
        action = action == null ? "" : action.trim();

        BOMDataEntryForm bomDataEntryForm = ( BOMDataEntryForm ) form;

        if( "bomBinDataEntry".equalsIgnoreCase( action ))
        {
            String qtyOutStr = request.getParameter("quantityOut");
            String lotIdStr  = request.getParameter("lotId");
            request.setAttribute("quantityOut", qtyOutStr);
            int qtyOutInt;
            int lotIdInt;
            try
            {
                qtyOutInt = Integer.parseInt( qtyOutStr);
            }
            catch( NumberFormatException e)
            {
                e.printStackTrace();
                qtyOutInt = 0;
            }

            try{
                lotIdInt = Integer.parseInt( lotIdStr);
            }catch( NumberFormatException e){
                e.printStackTrace();
                lotIdInt = -1;
            }

            if( bomDataEntryForm.isBomLotBinDataEntered() == false  && lotIdInt != -1)
            {
                ArrayList bomLotBinData = BOMDataEntryDAO.loadBomLotBinInfo( lotIdInt );
                if( bomLotBinData != null && bomLotBinData.size() > 0 )
                {
                    BOMDataEntryDAO.checkAssemblyReleaseExistsForBOMLot( lotIdInt, bomLotBinData );
                    bomDataEntryForm.setBomLotBinData( bomLotBinData );
                }
                else
                {
                    bomLotBinData = new ArrayList();
                    for( int i = 1; i <= qtyOutInt; i++   )
                    {
                        BOMLotBinData bomLotBin = new BOMLotBinData();
                        bomLotBin.setSerialNo( i);
                        bomLotBin.setBin1BalanceQty( "0");
                        bomLotBinData.add( bomLotBin);
                    }
                    bomDataEntryForm.setBomLotBinData( bomLotBinData);
                }
            }
            else
            {
                if( bomDataEntryForm.getBomLotBinData() == null )
                {
                    ArrayList bomLotBinData = new ArrayList();
                    for( int i = 1; i <= qtyOutInt; i++   )
                    {
                        BOMLotBinData bomLotBin = new BOMLotBinData();
                        bomLotBin.setSerialNo( i);
                        bomLotBin.setBin1BalanceQty( "0");
                        bomLotBinData.add( bomLotBin);
                    }
                    bomDataEntryForm.setBomLotBinData( bomLotBinData);
                }
                else if( qtyOutInt > bomDataEntryForm.getBomLotBinData().size() )
                {
                    ArrayList bomLotBinData = bomDataEntryForm.getBomLotBinData();
                    for( int i = bomLotBinData.size()+1; i <= qtyOutInt; i++   )
                    {
                        BOMLotBinData bomLotBin = new BOMLotBinData();
                        bomLotBin.setSerialNo( i);
                        bomLotBin.setBin1BalanceQty( "0");
                        bomLotBinData.add( bomLotBin);
                    }
                    bomDataEntryForm.setBomLotBinData( bomLotBinData);
                }
                else
                {
                    ArrayList bomLotBinDataTmp = new ArrayList();
                    ArrayList bomLotBinData = bomDataEntryForm.getBomLotBinData();
                    for( int i = 0; i < qtyOutInt; i++   )
                    {
                        BOMLotBinData bomLotBin = (BOMLotBinData) bomLotBinData.get(i);
                        bomLotBinDataTmp.add( bomLotBin);
                    }
                    bomLotBinData = null;
                    bomDataEntryForm.setBomLotBinData( bomLotBinDataTmp);
                }
            }
        }
        if( "saveBomBinDataEntry".equalsIgnoreCase( action ))
        {
            bomDataEntryForm.setBomLotBinDataEntered( true);
            ArrayList bomLotBinData = bomDataEntryForm.getBomLotBinData();
            for( int i = 0; i < bomLotBinData.size(); i++   )
            {
                BOMLotBinData bomLotBin = (BOMLotBinData)bomLotBinData.get(i);
                bomLotBin.setScrap( false );
                String isScrapped = request.getParameter("bomLotBinData["+i+"].scrap");
                if( isScrapped != null)
                {
                    bomLotBin.setScrap(true);
                }
                if( "-1".equalsIgnoreCase( bomDataEntryForm.getLotId() ) )
                {
                    bomLotBin.setBin1BalanceQty( bomLotBin.getBin1Qty() );
                }
            }
            bomDataEntryForm.setBomLotBinData( bomLotBinData);
            request.setAttribute("isSaved", "true");
            info.put("BOM Lot Bin Data saved succesfully", null);
        }
        request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        return ( mapping.findForward( actionForward ) );
    }
}